package com.ttdevs.ttrss;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.Window;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.webkit.WebViewClient;

public class ArticleView extends Activity {

	final Activity activity = this;
	private WebView webView;

	@SuppressLint("SetJavaScriptEnabled")
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		getWindow().requestFeature(Window.FEATURE_PROGRESS);

		setContentView(R.layout.activity_articleview);
		webView = (WebView) findViewById(R.id.webview);
		webView.getSettings().setJavaScriptEnabled(true);
		webView.getSettings().setSupportZoom(true);
		webView.setWebChromeClient(new ArticleViewChromeClient());
		webView.setWebViewClient(new ArticleViewClient());

		webView.loadUrl(getIntent().getStringExtra("URL"));
	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK && webView.canGoBack()) {
			webView.goBack();
			return true;
		} else {
			return super.onKeyDown(keyCode, event);
		}
	}

	private class ArticleViewClient extends WebViewClient {

		@Override
		public boolean shouldOverrideUrlLoading(WebView view, String url) {
			view.loadUrl(url);
			return true;
		}

		// @Override
		// public boolean shouldOverrideKeyEvent(WebView view, KeyEvent event) {
		// if (event.getKeyCode() == KeyEvent.KEYCODE_BACK
		// && webView.canGoBack()) {
		// webView.goBack();
		// return true;
		// } else {
		// return false;
		// }
		// }

	}

	private class ArticleViewChromeClient extends WebChromeClient {

		@Override
		public void onProgressChanged(WebView view, int newProgress) {
			activity.setTitle("Loading...");
			activity.setProgress(newProgress * 100);
			if (newProgress == 100) {
				activity.setTitle("finshed");
			}
		}

	}
}
